﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HHCommon;
using HHBLL;

namespace HHWeb.SystemManage.LeaveWord
{
    public partial class Dispose : Base
    {
        Help help = new Help();
        EncAndDec EncAndDec = new EncAndDec();
        HHBLL.LeaveWord lw = new HHBLL.LeaveWord();
        HHModel.LeaveWord lwmodel = new HHModel.LeaveWord();
        //变量定义
        public string Title = string.Empty;
        public string TypeName = string.Empty;
        public string Contents = string.Empty;
        public string Nickname = string.Empty;
        public string Telephone = string.Empty;
        public string Email = string.Empty;
        public int sort = 0;
        public int Status=1;
        public int LeaveWordStatus =0;
        public string Replyer = string.Empty;
        public string ReplyContents = string.Empty;
        public string ReplyTime = string.Empty;
        public string Creater = string.Empty;
        public string CreateTime = string.Empty;
        public string Command = "Add";//执行命令
        protected void Page_Load(object sender, EventArgs e)
        {
            string message = string.Empty;
            try
            {
                if (help.GetHttpMethodBool())
                {
                    switch (Request.Form["Command"].ToString())
                    {
                        case "Add":
                            message = Add();
                            break;
                        case "Update":
                            message = Update();
                            break;
                        default:
                            break;
                    }
                    if (message.EndsWith("成功！"))
                        MessageBox.ShowAndRedirect(this.Page, message, "List.aspx");
                    else
                        MessageBox.ResponseScript(this.Page, message, "javascript:history(-1)");
                }
                else
                {
                    if (help.IFPageParameter())
                    {
                        int LeaveWordID = int.Parse(Request.QueryString["ID"].ToString());
                        lwmodel = lw.GetModel(LeaveWordID);
                        if (lwmodel != null)
                        {
                            Title = lwmodel.Title;
                            TypeName = lwmodel.TypeName;
                            Contents = lwmodel.Contents;
                            Nickname = lwmodel.Nickname;
                            Telephone = lwmodel.Telephone;
                            Email = lwmodel.Email;
                            sort = Convert.ToInt32(lwmodel.sort);
                            Status = Convert.ToInt32(lwmodel.Status);
                            LeaveWordStatus = Convert.ToInt32(lwmodel.LeaveWordStatus);
                            Replyer = lwmodel.Replyer;
                            this.FCKeditor2.Value = lwmodel.ReplyContents;
                            ReplyTime = Convert.ToDateTime(lwmodel.ReplyTime).ToString("yyyy-MM-dd HH:mm:ss");
                            Creater = lwmodel.Creater;
                            CreateTime = Convert.ToDateTime(lwmodel.CreateTime).ToString("yyyy-MM-dd HH:mm:ss");
                            Command = "Update";
                        }
                        else
                        {
                            MessageBox.ShowAndRedirect(this.Page, "该数据不存在！", "List.aspx");
                            return;
                        }
                    }
                    else
                    {
                        Creater = EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
                        CreateTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        ReplyTime = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
            }
            catch
            {
                MessageBox.ResponseScript(this.Page, "操作错误，请联系管理员！", "javascript:history.go(-1)");
                return;
            }
        }

        #region //留言添加
        public string Add()
        {
            lwmodel.Title = Request.Form["Title"].ToString();
            lwmodel.TypeName = Request.Form["TypeName"].ToString();
            lwmodel.Contents = Request.Form["Contents"].ToString();
            lwmodel.Nickname = Request.Form["Nickname"].ToString();
            lwmodel.Telephone = Request.Form["Telephone"].ToString();
            lwmodel.Email = Request.Form["Email"].ToString();
            lwmodel.sort = int.Parse(Request.Form["sort"].ToString());
            lwmodel.Status = int.Parse(Request.Form["Status"].ToString());
            lwmodel.LeaveWordStatus = int.Parse(Request.Form["LeaveWordStatus"].ToString());
            lwmodel.Replyer = Request.Form["Replyer"].ToString();
            lwmodel.ReplyContents = this.FCKeditor2.Value;
            lwmodel.ReplyTime = Convert.ToDateTime(Request.Form["ReplyTime"].ToString());
            lwmodel.Creater = Request.Form["Creater"].ToString();
            lwmodel.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"].ToString());
            if (lw.Add(lwmodel) > 0)
                return "添加成功！";
            else
                return "添加失败！";
        }
        #endregion

        #region //留言回复
        public string Update()
        {
            lwmodel.ID=int.Parse(Request.QueryString["ID"].ToString());
            lwmodel.Title = Request.Form["Title"].ToString();
            lwmodel.TypeName = Request.Form["TypeName"].ToString();
            lwmodel.Contents = Request.Form["Contents"].ToString();
            lwmodel.Nickname = Request.Form["Nickname"].ToString();
            lwmodel.Telephone = Request.Form["Telephone"].ToString();
            lwmodel.Email = Request.Form["Email"].ToString();
            lwmodel.sort = int.Parse(Request.Form["sort"].ToString());
            lwmodel.Status = int.Parse(Request.Form["Status"].ToString());
            lwmodel.LeaveWordStatus = int.Parse(Request.Form["LeaveWordStatus"].ToString());
            lwmodel.Replyer = Request.Form["Replyer"].ToString();
            lwmodel.ReplyContents = this.FCKeditor2.Value;
            lwmodel.ReplyTime = Convert.ToDateTime(Request.Form["ReplyTime"].ToString());
            lwmodel.Creater = Request.Form["Creater"].ToString();
            lwmodel.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"].ToString());
            if (lw.Update(lwmodel))
                return "修改成功！";
            else
                return "修改失败！";
        }
        #endregion
    }
}